package org.mapdroid.data.adt;

import java.util.LinkedList;
import java.util.List;

import org.mapdroid.utils.MdTimer;

import android.os.CountDownTimer;
import android.os.Parcel;
import android.os.Parcelable;

import com.google.android.maps.GeoPoint;

public class Parking extends MdService implements Runnable, Parcelable{
	private int availableParkingNumber;
	private int busyParkingNumber;
	private int parkingNumber;
	private float pricePerHour;
	private List<Book> parkingList;
	
	//Constructor with parameters
	public Parking(	String name, 
					String address, 
					String phoneNumber, 
					String description, 
					int photo, 
					String link, 
					GeoPoint position,
					int parkingNumber,
					float pricePerHour){
		
		super(name, address, phoneNumber, description, photo, link, MdService.PARKING, position);
		
		this.parkingNumber = parkingNumber;
		parkingList = new LinkedList<Book>();
		this.availableParkingNumber = this.parkingNumber;
		this.busyParkingNumber = 0;
		this.pricePerHour = pricePerHour;
	}
	
	//Constructor with parameters
	public Parking(	String name, 
					String address, 
					String phoneNumber, 
					String description, 
					int photo, 
					String link, 
					GeoPoint position,
					int parkingNumber,
					int availableNumber,
					float pricePerHour,
					List<Book> list){
		
		super(name, address, phoneNumber, description, photo, link, MdService.PARKING, position);
		
		this.parkingNumber = parkingNumber;
		this.availableParkingNumber = availableNumber;
		this.busyParkingNumber = 0;
		this.pricePerHour = pricePerHour;
		this.parkingList = list;
	}
	
	public int getParkingNumber(){
		return this.parkingNumber;
	}
	
	public int getAvailableParkingNumber(){
		return this.availableParkingNumber;
	}
	
	public float getPricePerHour(){
		return this.pricePerHour;
	}
	
	public List<Book> getParkingList(){
		return this.parkingList;
	}
	
	//Returns the parking index or -1 if there is not available
	public Book bookingIn(String key){
		Book b = new Book(key);
		parkingList.add(b);
			
		availableParkingNumber--;
		busyParkingNumber++;
			
		Thread t = new Thread(key);
		t.start();
		return b;
	}
	
	public boolean bookingOut(String key){
		if(key == null || key.length()<1)
			return false;
		
		for(int i=0; i<this.parkingNumber; i++){
			if(parkingList.get(i).getPersonId().contentEquals(key)){
				parkingList.remove(i);
				availableParkingNumber++;
				busyParkingNumber--;
				return true;
			}
		}
		
		return false;
	}

	@Override
	public void run() {
		final String key = Thread.currentThread().getName();
		// TODO Auto-generated method stub
		//Create a Timer associated with a personId that delete his book after 
		//30 minutes (1.800.000 milliseconds)
		new CountDownTimer(MdTimer.TIMER_VALUE, 1000){
			@Override
			public void onFinish() {
				// TODO Auto-generated method stub
				bookingOut(key);
			}

			@Override
			public void onTick(long millisUntilFinished) {}
		};

	}

	@Override
	public int describeContents() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {
		// TODO Auto-generated method stub
		dest.writeString(this.name);
		dest.writeString(this.address);
		dest.writeString(this.phoneNumber);
		dest.writeString(this.description);
		dest.writeInt(this.photo);
		dest.writeList(this.gallery);
		dest.writeString(this.link);
		dest.writeDouble(this.position.getLatitudeE6());
		dest.writeDouble(this.position.getLongitudeE6());
		
		dest.writeInt(this.parkingNumber);
		dest.writeInt(this.availableParkingNumber);
		dest.writeFloat(this.pricePerHour);
		dest.writeList(this.parkingList);
	}
	
	public static final Parcelable.Creator<Parking> CREATOR
			= new Parcelable.Creator<Parking>(){
		@Override
		public Parking createFromParcel(Parcel in) {
			String name = in.readString();
			String address = in.readString();
			String phone = in.readString();
			String description = in.readString();
			int photo = in.readInt();
			List<Integer> outVal = new LinkedList<Integer>();
			in.readList(outVal, getClass().getClassLoader());
			String link = in.readString();
			GeoPoint point = new GeoPoint((int)in.readDouble(), (int)in.readDouble());
			
			int number = in.readInt();
			int availableNumber = in.readInt();
			float price = in.readFloat();
			List<Book> list = new LinkedList<Book>();
			in.readList(list, getClass().getClassLoader()); 
			
			return new Parking(name, address, phone, description, photo, link, point, number, availableNumber, price, list);
		}

		@Override
		public Parking[] newArray(int size) {
			return new Parking[size];
		}
	};
	
}